/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#include "sg_local.h"


/*
 ==================
 SG_SetConfigString
 ==================
*/
void SG_SetConfigString (int index, const char *string){

	if (index < 0 || index >= MAX_CONFIG_STRINGS)
		odSystem->Error(false, "SG_SetConfigString: bad index (%i)", index);

	if (!string)
		string = "";

	if (level.configStrings[index]){
		// Don't bother updating if no change
		if (!Str_Compare(level.configStrings[index], string))
			return;

		// Free the string
		odMemory->Free(level.configStrings[index]);
	}

	// Copy the string
	level.configStrings[index] = odMemory->CopyString(string, TAG_SERVER);

	// Send it to all the clients if we aren't spawning the server
	if (!level.spawned)
		return;

	sgi->BroadcastReliableCommand("cs %i \"%s\"", index, string);
}


// ============================================================================


/*
 ==================
 SG_FindIndex
 ==================
*/
static int SG_FindIndex (const char *name, int start, int max){

	int		index;

	if (!name || !name[0])
		return 0;

	for (index = 1; index < max; index++){
		if (!level.configStrings[start + index])
			break;

		if (!Str_ICompare(level.configStrings[start + index], name))
			return index;
	}

	if (index == max)
		odSystem->Error(false, "SG_FindIndex: overflow");

	SG_SetConfigString(start + index, name);

	return index;
}

/*
 ==================
 SG_AnimationIndex
 ==================
*/
int SG_AnimationIndex (const char *name){

	return SG_FindIndex(name, CS_ANIMATIONS, MAX_GAME_ANIMATIONS);
}

/*
 ==================
 SG_ModelIndex
 ==================
*/
int SG_ModelIndex (const char *name){

	return SG_FindIndex(name, CS_MODELS, MAX_GAME_MODELS);
}

/*
 ==================
 SG_ClipModelIndex
 ==================
*/
int SG_ClipModelIndex (const char *name){

	return SG_FindIndex(name, CS_CLIP_MODELS, MAX_GAME_CLIP_MODELS);
}

/*
 ==================
 SG_MaterialIndex
 ==================
*/
int SG_MaterialIndex (const char *name){

	return SG_FindIndex(name, CS_MATERIALS, MAX_GAME_MATERIALS);
}

/*
 ==================
 SG_MaterialLightIndex
 ==================
*/
int SG_MaterialLightIndex (const char *name){

	return SG_FindIndex(Str_VarArgs("light %s", name), CS_MATERIALS, MAX_GAME_MATERIALS);
}

/*
 ==================
 SG_MaterialNoMipIndex
 ==================
*/
int SG_MaterialNoMipIndex (const char *name){

	return SG_FindIndex(Str_VarArgs("noMip %s", name), CS_MATERIALS, MAX_GAME_MATERIALS);
}

/*
 ==================
 SG_SoundShaderIndex
 ==================
*/
int SG_SoundShaderIndex (const char *name){

	return SG_FindIndex(name, CS_SOUND_SHADERS, MAX_GAME_SOUND_SHADERS);
}
